﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Engine.Managers {
    import flash.events.*;
    import Engine.Transactions.*;
    import zasp.Classes.*;
    import flash.display.*;
    import flash.utils.*;

    public class ZaspManager {

        public static const DEFAULT_SAMPLE_RATE:int = 5000;

        protected var m_reportInterval:Number = 5000;
        protected var m_batchSize:int = 4;
        protected var m_active:Boolean = false;
        protected var m_sendFPSMessage:Boolean = false;
        protected var m_batchedReports:Array;
        protected var m_baseTracker:ZaspTrackerBase;
        protected var m_gpiTracker:ZaspTrackerGPI = null;
        protected var m_reportStart:Number;
        protected var m_previousFrameStart:Number;
        protected var m_frameStart:Number = 0;
        protected var m_frameEnd:Number = 0;
        protected var m_gameStateActive:Boolean = true;
        protected var m_gameDetailCallback:Function = null;
        protected var m_gameNumberCallback:Function = null;

        public function ZaspManager(){
            this.m_batchedReports = [];
            super();
            var _local1:int = int(GlobalEngine.getFlashVar("zaspSampleRate"));
            if (_local1 > 0){
                if ((((_local1 == 1)) || (((Math.random() * _local1) <= 1)))){
                    this.activate();
                };
            };
            _local1 = int(GlobalEngine.getFlashVar("zaspSamplePeriod"));
            if (_local1 > 0){
                this.m_reportInterval = _local1;
            };
            this.m_gameDetailCallback = this.gameDetail;
            this.m_gameNumberCallback = this.gameNumber;
        }
        protected function activate():void{
            this.m_active = true;
            this.m_baseTracker = new ZaspTrackerBase(String(GlobalEngine.getFlashVar("zaspSessionKey")), this.m_reportInterval);
            if (GlobalEngine.getFlashVar("zaspDetail") == "true"){
                this.m_baseTracker.activateDetailTracking();
            };
            if (GlobalEngine.getFlashVar("zaspFPS") == "true"){
                this.m_sendFPSMessage = true;
            };
            if ((GlobalEngine.getFlashVar("zaspGPI") == "true")){
                this.m_gpiTracker = new ZaspTrackerGPI(String(GlobalEngine.getFlashVar("zaspSessionKey")), this.m_reportInterval, false);
                this.m_gpiTracker.activate((GlobalEngine.getFlashVar("zaspSession") == "true"), (GlobalEngine.getFlashVar("zaspWait") == "true"), (GlobalEngine.getFlashVar("zaspWF") == "true"));
                trace("activating GPI reports");
            };
        }
        public function zaspWaitStart(_arg1:String, _arg2:String):int{
            if (((this.m_active) && (this.m_gpiTracker))){
                return (this.m_gpiTracker.zaspWaitStart(_arg1, _arg2));
            };
            return (0);
        }
        public function zaspWaitEnd(_arg1:int):void{
            if (((this.m_active) && (this.m_gpiTracker))){
                this.m_gpiTracker.zaspWaitEnd(_arg1);
            };
        }
        public function zaspPopup():void{
            if (((this.m_active) && (this.m_gpiTracker))){
                this.m_gpiTracker.popup();
            };
        }
        public function zaspInteractive():void{
            if (((this.m_active) && (this.m_gpiTracker))){
                this.m_gpiTracker.interactive();
            };
        }
        public function zaspGameStateDeactivate(_arg1:Event):void{
            this.m_gameStateActive = false;
        }
        public function zaspGameStateActivate(_arg1:Event):void{
            this.m_gameStateActive = true;
        }
        public function zaspOnMouseClick(_arg1:Event):void{
            this.zaspClick();
        }
        public function zaspIsGameActive():Boolean{
            return (this.m_gameStateActive);
        }
        public function zaspClick():void{
            if (((this.m_active) && (this.m_gpiTracker))){
                this.m_gpiTracker.click();
            };
        }
        public function trackTimingStart(_arg1:String):void{
            if (((this.m_active) && (this.m_gpiTracker))){
                this.m_gpiTracker.trackLoadTimingStart(_arg1);
            };
        }
        public function trackTimingStop(_arg1:String):void{
            if (((this.m_active) && (this.m_gpiTracker))){
                this.m_gpiTracker.trackLoadTimingStop(_arg1);
            };
        }
        public function preUpdate():void{
            if (this.m_active){
                this.m_previousFrameStart = this.m_frameStart;
                this.m_frameStart = getTimer();
            };
        }
        public function postUpdate():void{
            if (((this.m_active) && ((this.m_previousFrameStart > 0)))){
                this.m_frameEnd = getTimer();
                if (this.m_baseTracker.isDetailActive()){
                    this.updateGameState();
                };
                this.m_baseTracker.sample((this.m_frameStart - this.m_previousFrameStart), (this.m_frameEnd - this.m_frameStart));
                if (this.m_gpiTracker){
                    this.m_gpiTracker.setFPSData(this.m_baseTracker.f2f, this.m_baseTracker.oef, this.m_baseTracker.mem);
                    this.m_gpiTracker.sample();
                };
                if (!this.m_reportStart){
                    this.m_reportStart = this.m_frameEnd;
                } else {
                    if ((this.m_frameEnd - this.m_reportStart) > this.m_reportInterval){
                        this.cacheReport();
                        this.m_reportStart = this.m_frameEnd;
                    };
                };
            };
        }
        protected function cacheReport():void{
            var _local1:Object;
            if (this.m_baseTracker){
                if (this.m_sendFPSMessage){
                    _local1 = this.m_baseTracker.report();
                };
                if (_local1){
                    this.modifyReport(_local1);
                    this.m_batchedReports.push(_local1);
                };
            };
            _local1 = null;
            if (this.m_gpiTracker){
                _local1 = this.m_gpiTracker.report();
                if (_local1){
                    this.modifyReport(_local1);
                    this.m_batchedReports.push(_local1);
                };
            };
            if (this.m_batchedReports.length >= this.m_batchSize){
                this.sendReports();
            };
        }
        protected function modifyReport(_arg1:Object):void{
            if (_arg1["type"] == "fps"){
                _arg1["constants"] = (_arg1["constants"] + " V:0");
                _arg1["string5"] = "";
                _arg1["string6"] = "";
                _arg1["string7"] = "";
                _arg1["string8"] = "";
                _arg1["number7"] = 0;
                _arg1["number8"] = 0;
            } else {
                if (_arg1["type"] == "GPI"){
                };
            };
        }
        protected function gameDetail():String{
            return ("");
        }
        protected function gameNumber():Number{
            return (0);
        }
        private function updateGameState():void{
            var _local1:Number = 0;
            if (this.m_gameNumberCallback != null){
                _local1 = this.m_gameNumberCallback();
            };
            var _local2 = "";
            if (this.m_gameDetailCallback != null){
                _local2 = this.m_gameDetailCallback();
            };
            this.m_baseTracker.setGameState(_local2, _local1);
        }
        public function sendReports():void{
            if (((this.m_active) && (TransactionManager.canSendTransactions()))){
                TransactionManager.addTransaction(new TSendZaspReport(this.m_batchedReports));
                this.m_batchedReports = new Array();
            };
        }
        public function set batchSize(_arg1:int):void{
            this.m_batchSize = _arg1;
        }
        public function set reportInterval(_arg1:Number):void{
            this.m_reportInterval = _arg1;
        }
        public function get zasp():ZaspTrackerBase{
            return (this.m_baseTracker);
        }
        public function set gameDetailCallback(_arg1:Function):void{
            this.m_gameDetailCallback = _arg1;
        }
        public function set gameNumberCallback(_arg1:Function):void{
            this.m_gameNumberCallback = _arg1;
        }
        public function addStageListeners(_arg1:DisplayObjectContainer):void{
            _arg1.addEventListener(Event.ACTIVATE, this.zaspGameStateActivate);
            _arg1.addEventListener(Event.DEACTIVATE, this.zaspGameStateDeactivate);
            _arg1.addEventListener(MouseEvent.CLICK, this.zaspOnMouseClick, false, 3);
        }
        public function forceActivateDebugTracking(_arg1:DisplayObjectContainer=null):void{
            if (this.m_active == false){
                this.activate();
            };
            this.m_baseTracker.activateDebugTracking(_arg1);
        }

    }
}//package Engine.Managers 
